<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 首页控制器
 */
class Index extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->assign("action", "index");
        $this->load->model('bbs_detail_model');
    }

    function index($parent_id) {
        $parent_id = decrypt_user_id($parent_id);
        $parent_user = $this->users_model->get_user_by_uid(intval($parent_id));
        if($parent_user){
            if($parent_user['status'] == 0){
                redirect('error/index');
            }else{
                $this->session->set_userdata('parent_id',$parent_id);
            }
        }
        
        $kw_arr = explode(",", $this->website_config['article_hot_key']);
        $search_word = $kw_arr[rand(0, count($kw_arr)-1)];
        //文章首页文章列表
        $articles_list = $this->article_list();
        $banners_list = $this->banners_round();
        $this->assign("search_word", $search_word);
        $this->assign("articles", $articles_list);
        $this->assign("banners", $banners_list);
        
        
        $this->display('article/index.html');
    }
    
    //文章首页轮播banner
    private function banners_round(){
        $list = $this->db->query("select * from tan_banner where type = 5 and status = 1 order by sort desc")->result_array();
        foreach ($list as $key=>$val){
            $list[$key]['banner_link'] = str_replace("www", "wx", $val['banner_link']);
        }
        return $list;
    }
    //文章首页文章列表
    private function article_list(){
//        $list = $this->memcached->get("www_hot_article");
//        if($list){
//            $list = unserialize($list);
//        }else{
            $query_params = "a.id,a.authorid,a.title,a.thumb,a.content,a.readnum,a.commentnum,a.create_time,u.head_img,u.nickname";
            $list = $this->db->query("select $query_params from tan_bbs_article a left join tan_users_info u on u.user_id = a.authorid where a.status = 1 and a.hot = 1 and a.show = 1 order by a.hot desc,a.sort desc limit 27")->result_array();
//        }
        
        foreach ($list as $key=>$val){
            $matches = array();
            preg_match_all("/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.bmp|\.jpeg]))[\'|\"].*?[\/]?>/i", $val['content'], $matches);
            $content_thumb = $matches[1][0] ? $matches[1][0] : "";
            $list[$key]['content_thumb'] = $content_thumb;
            $list[$key]['detail_url'] = site_url("p/".  encrypt_article_id($val['id']));
            $list[$key]['space_url'] = site_url("u/". encrypt_user_id($val['authorid']));
            $list[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
            $list[$key]['article_id'] = encrypt_article_id($val['id']);
        }
        return $list;
    }
}
